/*
 * Copyright (C), 2016-2020, 上海巧房信息技术有限公司
 * ClassName: MybatisConfig
 * Author:   liushihao
 * Date:     2020/12/8 4:20 PM
 * Description: //模块目的、功能描述
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名     修改时间      版本号        描述
 */
package com.qiaofang.generator.base.configuration;

import com.alibaba.druid.pool.DruidDataSource;
import com.google.common.collect.Maps;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.util.Map;

/**
 * 〈一句话功能简述〉<br>
 * 初始化动态数据源
 *
 * @author liushihao
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Configuration
public class DynamicDataSourceConfig {

    @Bean("master")
    @ConfigurationProperties(prefix = "spring.datasource.master")
    public DataSource master() {
        return new DruidDataSource();
    }


    @Primary
    @Bean("dynamicDataSource")
    public DataSource dynamicDataSource(DataSource master, DataSource master1) {
        com.qiaofang.generator.base.configuration.DynamicDataSource dynamicDataSource = new com.qiaofang.generator.base.configuration.DynamicDataSource();
        Map<Object, Object> dataSourceMap = Maps.newHashMap();
        dataSourceMap.put("master", master);
        dynamicDataSource.setDefaultDataSource(master);
        dynamicDataSource.setDataSources(dataSourceMap);
        return dynamicDataSource;
    }

}
